package com.wocao.controller.uniapp;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wocao.entity.ReqBody;
import com.wocao.entity.ResponseResult;
import com.wocao.entity.ZyCommunity;
import com.wocao.entity.ZyComplaintSuggest;
import com.wocao.service.ZyCommunityService;
import com.wocao.service.ZyComplaintSuggestService;
import com.wocao.utils.OssUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("uni-app/zyComplaintSuggest")
public class UniComplaintController {
    @Autowired
    private ZyComplaintSuggestService zyComplaintSuggestService;

    //小程序-根据业主id查询投诉建议
    @GetMapping("/selectAllByOwnerId/{userId}")
    public ResponseResult selectAllByOwnerId(@PathVariable String userId, @RequestParam(required = false, defaultValue = "", name = "comType") String comType) {
        return ResponseResult.ok(zyComplaintSuggestService.selectAllByOwnerId(userId, comType));
    }

    //小程序-添加
    @PostMapping("/complaintAdd")
    public ResponseResult<String> complaintAdd(@RequestBody ZyComplaintSuggest zyComplaintSuggest) {
        zyComplaintSuggestService.ComplaintAdd(zyComplaintSuggest);
        return ResponseResult.ok("添加成功");
    }

    //小程序-删除
    @DeleteMapping("/complaintDelete/{comId}")
    public ResponseResult<String> complaintDelete(@PathVariable String comId) {
        zyComplaintSuggestService.removeById(comId);
        return ResponseResult.ok("删除成功");
    }

    //小程序-文件上传
    @PostMapping("/sugUploadImages")
    public ResponseResult<String> sugUploadImages(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        OssUtils.upload(file.getInputStream(), filename);
        String url = OssUtils.getURL(filename);
        String substring = url.substring(0, url.indexOf("?"));
        return ResponseResult.ok(substring);
    }

    @Autowired
    private ZyCommunityService zyCommunityService;

    //小程序-查询所有小区
    @GetMapping("/communitys/{userId}")
    public ResponseResult<List<ZyCommunity>> selectAll(@PathVariable String userId) {
        List<ZyCommunity> list = zyCommunityService.selectAllByOwnerId(userId);
        return ResponseResult.ok(list);
    }

}
